package com.abc.parse;

import com.alibaba.druid.util.StringUtils;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ParseMain {


   public static void main(String[] args) throws Exception {

//      createSelect();

//      doGen();
//      System.out.println("flag =" + doCheck("0304500003_0228100001"));

   }

   private static boolean doCheck(String sceneCodes) {
      boolean flag = false;

      if (!StringUtils.isEmpty(sceneCodes)
              && sceneCodes.length() >= 5) {

         int size = sceneCodes.length();

         String msg = sceneCodes.substring(size - 5, size);

         if ("00001".equals(msg)) {
            return true;
         }
      }


      return flag;
   }

   public static void createSelect() {
      System.out.println("==================================================创建查询====================================================");
      PlainSelect plainSelect = new PlainSelect();
      //创建查询的表
      Table table = new Table("user");
//      table.setAlias(new Alias("t", false));
      plainSelect.setFromItem(table);
//      plainSelect.setUseBrackets(true);

      SelectExpressionItem selectExpressionItem = new SelectExpressionItem();
      selectExpressionItem.setExpression(new Column(table, "username"));
      plainSelect.addSelectItems(selectExpressionItem);

      //创建查询的列
//      List<String> selectColumnsStr = Arrays.asList("f1");
//      List<SelectItem> expressionItemList = selectColumnsStr.stream().map(item -> {
//         SelectExpressionItem selectExpressionItem = new SelectExpressionItem();
//         selectExpressionItem.setExpression(new Column(table, item));
//         return (SelectItem) selectExpressionItem;
//      }).collect(Collectors.toList());

//      System.out.println(plainSelect);


//      SelectExpressionItem selectExpressionItem = new SelectExpressionItem();
//      selectExpressionItem.setAlias(new Alias("count"));
//      Function function = new Function();
//      function.setName("count");
//      ExpressionList expressionList = new ExpressionList();
//      expressionList.setExpressions(Arrays.asList(new Column(table, "f1")));
//      function.setParameters(expressionList);
//      selectExpressionItem.setExpression(function);
//      expressionItemList.add(selectExpressionItem);
//      plainSelect.setSelectItems(expressionItemList);

      AtomicInteger atomicInteger = new AtomicInteger(1);

      List<Join> joinList = Stream.of(new String[1]).map(item -> {

         Join join = new Join();
         join.setLeft(true);
         join.setOuter(true);

         Table joinTable = new Table();
         joinTable.setName("order");
         joinTable.setAlias(new Alias("t" + atomicInteger.get()));
         join.setRightItem(joinTable);

         EqualsTo equalsTo = new EqualsTo();
         equalsTo.setLeftExpression(new Column(table, "f1"));
         equalsTo.setRightExpression(new Column(joinTable, "f2"));
         join.setOnExpression(equalsTo);

         //条件
//         EqualsTo leftEqualsTo = new EqualsTo();
//         leftEqualsTo.setLeftExpression(new Column(joinTable, "f1"));
//         StringValue stringValue = new StringValue("1222121");
//         leftEqualsTo.setRightExpression(stringValue);
//         plainSelect.setWhere(leftEqualsTo);

         return join;
      }).collect(Collectors.toList());

      plainSelect.setJoins(joinList);

//      EqualsTo rightEqualsTo = new EqualsTo();
//      rightEqualsTo.setLeftExpression(new Column(table, "f2"));
//      StringValue stringValue1 = new StringValue("122212111111");
//      rightEqualsTo.setRightExpression(stringValue1);
//      OrExpression orExpression = new OrExpression(leftEqualsTo, rightEqualsTo);
//      plainSelect.setWhere(orExpression);

      System.out.println(plainSelect);

      //分组
//      GroupByElement groupByElement = new GroupByElement();
//      groupByElement.setGroupByExpressions(Arrays.asList(new Column(table, "f1")));
//      plainSelect.setGroupByElement(groupByElement);
//      System.out.println(plainSelect);

      //排序
//      OrderByElement orderByElement = new OrderByElement();
//      orderByElement.setAsc(true);
//      orderByElement.setExpression(new Column(table, "f1"));
//      OrderByElement orderByElement1 = new OrderByElement();
//      orderByElement1.setAsc(false);
//      orderByElement1.setExpression(new Column(table, "f2"));

      //分页
//      Limit limit = new Limit();
//      limit.setRowCount(new LongValue(2));
//      limit.setOffset(new LongValue(10));
//      plainSelect.setLimit(limit);
//      plainSelect.setOrderByElements(Arrays.asList(orderByElement, orderByElement1));
//      System.out.println(SQLFormatterUtil.format(plainSelect.toString()));
//      System.out.println("==================================================创建查询====================================================");
   }
}
